import {
  exportUrl,
  getProcessCodeList,
  getProcessList,
} from "@/api/process/mesProcessMaterial";
import { getDict } from '@/api/common';
import url from "@/axios/url";
import { exportExcel } from "@/utils/judge";
import excelUpload from "@/components/excel-upload/excelUpload";

const { mesUrl } = url();

export const pageConfig = {
  data () {
    const _this = this;
    return {
      operateList: [
        {
          name: $i18n.t("common.export"),
          type: "primary",
          icon: "el-icon-download",
          handleName: "export", //点击时返回的标识
          url: exportUrl,
        },
        {
          name: $i18n.t("common.add"),
          type: "primary",
          icon: "el-icon-circle-plus-outline",
          handleName: "add", //点击时返回的标识
        },
        {
          name: $i18n.t("common.delete"),
          type: "danger",
          icon: "el-icon-delete",
          handleName: "deleteBatch", //点击时返回的标识
          isMultiple: true,
        },
        {
          // 导入文件
          render: (h, scope) => {
            // scope列表上选择的数据
            const uploadUrl = `${mesUrl}mesProcessMaterial/importData`;
            return (
              <excelUpload
                action={uploadUrl}
                on={{
                  handleUploadError: (err) => {
                    // 失败的回调
                    console.log(err);
                    _this.refresh();
                  },
                  handleUploadSuccess: (res) => {
                    _this.refresh();
                    //成功 的回调
                  },
                }}
              />
            );
          },
        },
        {
          name: $i18n.t("common.downloadTemplate"),
          type: "success",
          icon: "el-icon-download",
          handleName: "exportExcel",
        },
      ],
    };
  },
  computed: {
    formDescDialog () {
      const _this = this;
      return {
        span: 24,
        disabled: false,
        isShowSubmitBtn: true,
        labelPosition: "top",
        formDesc: {
          type: {
            type: "select",
            label: $i18n.t("mesProcessMaterial.type"),
            layout: 24,
            required: true,
            prop: {
              value: "value",
              text: "description",
            },
            options: async (data) => {
              const list = await getDict('process_material_type');
              return list.data.result;
            },
          },
          technologyName: {  // 工艺路线
            label: $i18n.t("mesProcessMaterial.technologyRoute"),
            layout: 12,
            required: true,
            optionsLinkageFields: ['technologyProcess'],
            vif: (data) => {
              return data.type === '0';
            }
          },
          prodName: {  // 产品
            type: "select",
            label: $i18n.t("mesProcessMaterial.product"),
            layout: 12,
            required: true,
            vif: (data) => {
              return data.type === '1';
            }
          },
          seriesName: {  // 系列
            type: "select",
            label: $i18n.t("mesProcessMaterial.seriesName"),
            layout: 12,
            required: true,
            vif: (data) => {
              return data.type === '2';
            }
          },
          technologyProcess: {  // 工艺工序
            type: "select",
            label: $i18n.t("mesProcessMaterial.process"),
            layout: 12,
            required: true,
            prop: {
              value: "processCode",
              text: "processName",
            },
            options: _this.processOptions,
            vif: (data) => {
              return data.type === '0' && data.technologyId;
            }
          },
          otherProcess: {  // 其他工序
            type: "select",
            label: $i18n.t("mesProcessMaterial.process"),
            layout: 12,
            required: true,
            prop: {
              value: "processCode",
              text: "processName",
            },
            options: async (data) => {
              const list = await getProcessList();
              return list.data.result;
            },
            vif: (data) => {
              return data.type !== '0' && data.type;
            }
          },
          technologyType: {  // 工艺类型
            type: "input",
            label: $i18n.t("mesProcessMaterial.technologyType"),
            layout: 12,
            required: false,
            disabled: true,
            vif: (data) => {
              return data.type === '0' && data.technologyId;
            },
          },
          actualCode: {  // 产品/系列编码
            type: "input",
            label: $i18n.t("mesProcessMaterial.productSeriesCode"),
            layout: 12,
            required: false,
            disabled: true,
            vif: (data) => {
              return data.type === '0' && data.technologyId;
            }
          },
          actualName: {  // 产品/系列名称
            type: "input",
            label: $i18n.t("mesProcessMaterial.productSeriesName"),
            layout: 12,
            required: false,
            disabled: true,
            vif: (data) => {
              return data.type === '0' && data.technologyId;
            }
          },
          versionNumber: {  // 版本
            type: "input",
            label: $i18n.t("mesProcessMaterial.version2"),
            layout: 12,
            required: false,
            disabled: true,
            vif: (data) => {
              return data.type === '0' && data.technologyId;
            }
          },
          prodCode: {  // 产品编码
            type: "input",
            label: $i18n.t("mesProcessMaterial.prodCode"),
            layout: 12,
            required: false,
            disabled: true,
            vif: (data) => {
              return data.type === '1';
            }
          },
          prodSpecification: {  // 产品规格
            type: "input",
            label: $i18n.t("mesProcessMaterial.productSpecification"),
            layout: 12,
            required: false,
            disabled: true,
            vif: (data) => {
              return data.type === '1';
            }
          },
          seriesCode: {  // 系列编码
            type: "input",
            label: $i18n.t("mesProcessMaterial.seriesCoding"),
            layout: 12,
            required: true,
            disabled: true,
            vif: (data) => {
              return data.type === '2';
            }
          },
          materialList: {
            label: "",
            layout: 24,
            type: "table-editor",
            vif: (data) => {
              return this.dialogStatus === 'add'
            },
            attrs: {
              topBtnRender: (h, solt) => {
                return (
                  <el-button
                    type="primary"
                    onClick={() => _this.handleAddMaterial('materialList')}>
                    {$i18n.t("common.add")}
                  </el-button>
                )
              },
              isShowAdd: false,
              columns: [
                {
                  type: "index",
                  width: 50
                },
                {
                  prop: "materialCode",  // 物料编码
                  label: $i18n.t("mesProcessMaterial.materialCode"),
                  minWidth: 200,
                  align: 'center',
                },
                {
                  prop: "materialName",  // 物料名称
                  label: $i18n.t("mesProcessMaterial.materialName"),
                  minWidth: 120,
                  align: 'center',
                },
                {
                  prop: "materialSpecification",  // 物料规格
                  label: $i18n.t("mesProcessMaterial.materialSpecification"),
                  minWidth: 120,
                  align: 'center',
                },
                {
                  prop: "isKey",  // 是否关键物料
                  label: $i18n.t("mesProcessMaterial.isKey"),
                  minWidth: 120,
                  align: 'center',
                  content: {
                    type: 'el-select',
                    options: [
                      { text: $i18n.t('common.yes'), value: '1' },
                      { text: $i18n.t('common.no'), value: '0' }
                    ],
                    change: (data, _, index) => {
                      // 不是关键物料，不可编辑 是否重复和物料正则两列
                      if (data === '0') {
                        this.formDataDialog['materialList'][index]['regularContent'] = '';
                        this.formDataDialog['materialList'][index]['regularName'] = '';
                        this.formDataDialog['materialList'][index]['regularCode'] = '';
                        delete this.formDataDialog['materialList'][index]['isRepeat'];
                      } else {
                        this.formDataDialog['materialList'][index]['isRepeat'] = '1'
                      }
                    }
                  }
                },
                {
                  prop: "isRepeat",
                  label: $i18n.t("mesProcessMaterial.isRepeat"),
                  minWidth: 120,
                  align: 'center',
                  content: {
                    type: 'el-select',
                    attrs: ({ row }) => {
                      return {
                        disabled: row?.isKey === '0'
                      };
                    },
                    options: [
                      { text: $i18n.t('common.yes'), value: '1' },
                      { text: $i18n.t('common.no'), value: '0' }
                    ],
                  }
                },
                {
                  prop: "regularContent",
                  label: $i18n.t("mesProcessMaterial.materialRegular"),
                  minWidth: 180,
                  align: 'center',
                  isShowSlot: true,
                  content: {
                    type: 'search-bar',
                    attrs: ({ row }) => {
                      return {
                        disabled: row.isKey === '0'
                      };
                    },
                    change: (data, _, index) => {
                      _this.selectedRowIndex = index;
                      _this.regularDialogVisible = true;
                    },
                  },
                },
                {
                  prop: "remark",
                  label: $i18n.t("common.remark"),
                  minWidth: 180,
                  align: 'center',
                  content: {
                    type: 'el-input',
                    attrs: {
                      placeholder: $i18n.t('mesProcessMaterial.enterRemarks')
                    }
                  }
                },
              ],
              // 新行默认值
              newColumnValue: {
                lineNum: 0,
                count: 0
              }
            },
          },
          materialCode: {  // 物料编码
            type: "input",
            label: $i18n.t("mesProcessMaterial.materialCode"),
            layout: 12,
            vif: (data) => {
              return ['edit', 'detail'].includes(this.dialogStatus);
            }
          },
          materialName: {  // 物料名称
            type: "input",
            label: $i18n.t("mesProcessMaterial.materialName"),
            layout: 12,
            disabled: true,
            vif: (data) => {
              return ['edit', 'detail'].includes(this.dialogStatus);
            }
          },
          materialSpecification: {  // 物料规格
            type: "input",
            label: $i18n.t("mesProcessMaterial.materialSpecification"),
            layout: 12,
            disabled: true,
            vif: (data) => {
              return ['edit', 'detail'].includes(this.dialogStatus);
            }
          },
          isKey: {  // 是否关键物料
            type: "select",
            label: $i18n.t("mesProcessMaterial.isKey"),
            layout: 12,
            options: [
              { text: $i18n.t('common.yes'), value: '1' },
              { text: $i18n.t('common.no'), value: '0' }
            ],
            vif: (data) => {
              return ['edit', 'detail'].includes(this.dialogStatus);
            }
          },
          isRepeat: {
            type: "select",
            label: $i18n.t("mesProcessMaterial.isRepeat"),
            layout: 12,
            options: [
              { text: $i18n.t('common.yes'), value: '1' },
              { text: $i18n.t('common.no'), value: '0' }
            ],
            disabled: (data) => {
              if (data.isKey === '0') {
                this.formDataDialog.isRepeat = null
                this.formDataDialog.regularContent = ''
              } else {
                this.formDataDialog.isRepeat = '1'
              }
              return data.isKey === '0';
            },
            vif: (data) => {
              return ['edit', 'detail'].includes(this.dialogStatus);
            }
          },
          regularContent: {
            type: "input",
            label: $i18n.t("mesProcessMaterial.materialRegular"),
            layout: 12,
            vif: (data) => {
              return ['edit', 'detail'].includes(this.dialogStatus);
            }
          },
        },
      };
    }
  }
};

// 行末按钮配置
export const rowConfig = [
  {
    name: $i18n.t("common.edit"),
    type: "primary",
    class: "row-btn-primary",
    handleName: "edit", //点击时返回的标识
  },
  {
    name: $i18n.t("common.details"),
    type: "info",
    class: "row-btn-info",
    handleName: "detail", //点击时返回的标识
  },
  {
    name: $i18n.t("common.delete"),
    type: "danger",
    class: "row-btn-danger",
    handleName: "delete", //点击时返回的标识
  },
];

// 搜索条件配置
export const searchConfig = {
  inline: true,
  formDesc: {
    type: {
      type: "select",
      label: $i18n.t("mesProcessMaterial.type"),
      prop: {
        value: "value",
        text: "description",
      },
      options: async (data) => {
        const list = await getDict('process_material_type');
        return list.data.result;
      },
    },
    processCode: {
      type: "input",
      label: $i18n.t("mesProcessMaterial.processCode"),
    },
    processName: {
      type: "select",
      label: $i18n.t("mesProcessMaterial.processName"),
      prop: {
        value: "processName",
        text: "processName",
      },
      options: async (data) => {
        const list = await getProcessCodeList();
        return list.data.result;
      },
    },
    materialCode: {
      type: "input",
      label: $i18n.t("mesProcessMaterial.materialCode"),
      filterable: true,
    },
    materialName: {
      type: "input",
      label: $i18n.t("mesProcessMaterial.materialName"),
    },
    materialSpecification: {
      type: "input",
      label: $i18n.t("mesProcessMaterial.materialSpecification"),
    },
    prodCode: {
      type: "input",
      label: $i18n.t("mesProcessMaterial.prodCode"),
    },
    prodName: {
      type: "input",
      label: $i18n.t("mesProcessMaterial.prodName"),
    },
    seriesCode: {
      type: "input",
      label: $i18n.t("mesProcessMaterial.seriesCoding"),
    },
    seriesName: {
      type: "input",
      label: $i18n.t("mesProcessMaterial.seriesName"),
    },
    technologyCode: {
      type: "input",
      label: $i18n.t("mesProcessMaterial.technologyCode"),
    },
    technologyName: {
      type: "input",
      label: $i18n.t("mesProcessMaterial.technologyName"),
    },
  },
};


//导入数据
export const importFormDescDialog = {
  span: 24,
  disabled: false,
  isShowSubmitBtn: true,
  formDesc: {
    model: {
      type: "button",
      label:"",// $i18n.t("mesProcessMaterial.model"),
      layout: 24,
      required: false,
      title: $i18n.t("mesProcessMaterial.downloadTemplate"),
      style: {
        color: "#1867c0",
        fontSize: "14px",
      },
      on: {
        click: () => {
          exportExcel(downloadTemplateUrl);
        },
      },
    },
    file: {
      type: "input",
      label: $i18n.t("mesProcessMaterial.chooseFile"),
      layout: 24,
      // required: true,
    },
  },
};
